<template>
    <div>
        <Header-Two></Header-Two>
        <div class="middleAll">
            <div class="box" style="margin-bottom: -30px">
                <div class="shoppingTop"><span>我的购物车</span><span>共四门课程</span></div>
                <div class="shoppingCar">

                    <a-row :gutter="[24,8]"
                           style="height: 50px;background-color: #FAFAFA;margin: 20px 0px;line-height: 50px;border: 1px solid #eeeeee">
                        <a-col :span="6">
                            <div>
                                <a-checkbox :indeterminate="indeterminate" :checked="checkAll"
                                            @change="onCheckAllChange">
                                    全选
                                </a-checkbox>
                            </div>
                        </a-col>
                        <a-col :span="5">课程名称</a-col>
                        <a-col :span="5">原价</a-col>
                        <a-col :span="4">优惠</a-col>
                        <a-col :span="4">操作</a-col>

                    </a-row>
                    <!--                    列表详情-->
                    <a-row :gutter="[24,8]"
                           style="height: 120px;border: 1px solid #e8e8e8;margin: 20px 0px;line-height: 120px"
                           v-for="(item,index) in cartList" :key="index">
                        <a-col :span="6">
                            <a-checkbox :checked="checkedList.some(item1=>item1==item)" @change="onChange($event,item)">
                                <img :src="item.course.bannerFileUrl" alt=""
                                     style="width: 156px;height: 95px;margin-top: -10px">
                            </a-checkbox>
                        </a-col>
                        <a-col :span="5">{{item.course.courseTitle}}</a-col>
                        <a-col :span="5">
                            <del>￥{{item.course.coursePrice}}</del>
                            <span style="color: red">￥{{item.course.discountPrice}}</span></a-col>
                        <a-col :span="4">
                            <a-tag color="#f50">
                                {{item.course.discountDesc}}
                            </a-tag>
                        </a-col>
                        <a-col :span="4">
                            <a-icon type="delete" style="color: #F50" @click="deleteCart(item.shoppingCartId,item)"/>
                        </a-col>
                    </a-row>

                    <!-- 上一部分的内容-->
                    <!--总计内容框部分-->
                    <a-row :gutter="[24,8]" class="moneyTotal">
                        <a-col :span="18">
                            <div>
                                <a-checkbox :indeterminate="indeterminate" :checked="checkAll"
                                            @change="onCheckAllChange">
                                    全选

                                </a-checkbox>
                            </div>
                        </a-col>
                        <a-col :span="6" style="padding: 0px;margin: 0px;"><span>合计:</span><span style="color: red;font-size: 18px">￥{{money}}</span>
                            <div class="sumit">提交</div>
                        </a-col>


                    </a-row>

                    <!-- 总计内容部分结束-->
<!--                    <a-checkbox-group v-model="checkedList" />-->
                    <!--                   <a-checkbox-group v-model="checkedList" :options="plainOptions" @change="onChange" />-->
                </div>
            </div>

        </div>

        <Footer></Footer>
    </div>
</template>

<script>
    import HeaderTwo from "./HeaderTwo";
    import Footer from "./Footer";

    export default {
        name: "ShoppingCart.vue",
        data() {
            return {
               checkedList:[],
                money:0

            }
        },

        computed:{
          cartList(){
              return this.$store.state.cart.cartList

          },
            checkAll:{
              get(){
                  return this.$store.getters["cart/checkAll"]
              },
                set(){}
            },
            indeterminate:{
              get(){
                  return this.$store.getters["cart/indeterminate"]
              },
              set(){}
            },

        },
        watch:{
            checkedList(){
                this.money=0
                for(let item of this.checkedList){
                    if(item.course.isDiscount){
                        this.money+=Number(item.course.discountPrice)
                    }else{
                        this.money+=item.course.coursePrice
                    }
                }

            }
        },
        methods: {
            onChange(e,item) {
                console.log(e.target.checked)
                if(e.target.checked){

                    if(!this.checkedList.some(item2=> item2==item)){
                        this.checkedList.push(item);
                        this.$store.commit("cart/changCheckList",this.checkedList);
                    }
                    console.log(this.checkedList)

                }else{
                    this.checkedList.splice(this.checkedList.indexOf(item),1);
                    this.$store.commit("cart/changCheckList",this.checkedList);
                    console.log(this.checkedList)
                }



            },
            onCheckAllChange(e) {

                    this.checkedList = e.target.checked ? this.cartList.concat() : [];
                    this.indeterminate = false;
                    this.checkAll = e.target.checked;
                this.$store.commit("cart/changCheckList",this.checkedList);

            },

            deleteCart(m,item){
                this.$store.dispatch("cart/deleteCartoo",m)
                this.$store.commit("cart/changeCartList", this.cartList.filter(item3=>item3.shoppingCartId!=item.shoppingCartId))
                console.log( this.cartList.filter(item3=>item3.shoppingCartId==item.shoppingCartId))

            }
        },


        components: {
            HeaderTwo,
            Footer
        },
        //登陆成功后个人的数据
        created() {
            this.$store.dispatch("cart/initCartList")
            this.checkedList=this.$store.state["cart/checkList"] || []
        },
        //登陆成功后个人得数据结束
    }
</script>


<style scoped lang="less">
body {
    width: 100%;
}
    .middleAll {
        min-height: calc(100vh - 336px);
        border-top: 1px solid #eaeaea;
        /*overflow: hidden;*/
        box-sizing: border-box;

        .box {
            width: 1200px;
            margin: 0 auto;
            padding: 0px 20px;

            .shoppingTop {
                height: 20px;
                padding: 20px 0px;

                span:nth-of-type(1) {
                    font-size: 16px;
                    font-weight: 400;
                }

                span:nth-of-type(2) {
                    font-size: 12px;
                    margin-left: 10px;
                }
            }

            .shoppingCar {
                height: 500px;

            }
        }
    }

    .sumit {
        width: 120px;
        height: 60px;
        background: #999999;
        float: right;
        text-align: center;
        line-height: 60px;
        font-size: 20px;
        color: #FFFFFF;
    }
    .moneyTotal {
        height: 60px;
        background-color: #EEEEEE;
        line-height: 60px;
        border: 1px solid #eeeeee;
    }

</style>